<?php
namespace app\admin\controller;

use app\admin\model\OrganizeModel;

class OrganizeController extends BaseController
{
    
    public function index(){
        $organize = OrganizeModel::select()->toArray();
        $organize_list = array();
        foreach($organize as $i){
            if($i['master'] == 0){
                $i['Hierarchy'] = 0;
                $organize_list[] = $i;
            }
        }

        for($i=0;$i<count($organize);$i++){
            if($organize_list[$i] != null){
                $og = $organize_list[$i];
                $py = 0;
                foreach($organize as $ite){
                    if($ite['master'] == $og['og_id']){
                        for($c=count($organize_list);$c>($i+1+$py);$c--){
                            $organize_list[$c] = $organize_list[$c-1];
                        }
                        $flag = $ite['master'];
                        $hierarchy = 0;
                        while($flag != 0){
                            foreach($organize as $item){
                                if($flag == $item['og_id']){
                                    $flag = $item['master'];
                                    break;
                                }
                            }
                            $hierarchy++;
                        }

                        $ite['Hierarchy'] = $hierarchy;
                        $organize_list[$i+1+$py] = $ite;
                        $py++;
                    }
                }
            } 
        }
        $maxHierarchy = 0;
        for($i=0;$i<count($organize_list);$i++){
            if($organize_list[$i]['Hierarchy'] > $maxHierarchy){
                $maxHierarchy = $organize_list[$i]['Hierarchy'];
            }
        }

        for($i=0;$i<count($organize_list);$i++){
            $organize_list[$i]['fontstyle'] = '';
        }

        for($i=0;$i<=$maxHierarchy;$i++){
            $style = ['├─ ','│　','└─ ','　　'];
            $starth = 0;
            $endh = 0;
            $dc = array();
            $nc = array();
            for($ii=0;$ii<count($organize_list);$ii++){
                if($organize_list[$ii]['Hierarchy'] == $i){
                    $c = $organize_list[$ii];
                    $c['id'] = $ii;
                    $nc[] = $c;
                }
            }
            if(count($nc)>1){
                for($ii=1;$ii<count($nc);$ii++){
                    if($nc[$ii]['master'] != $nc[$ii-1]['master']){
                        $dc[] = [$nc[$ii-1]['id'],$nc[$ii]['id']];
                    }
                }
            }
            for($ii=0;$ii<count($organize_list);$ii++){
                if($organize_list[$ii]['Hierarchy'] == $i){
                    $starth = $ii;
                    break;
                }
            }
            for($ii=0;$ii<count($organize_list);$ii++){
                if($organize_list[$ii]['Hierarchy'] == $i){
                    if($ii > $endh){
                        $endh = $ii;
                    }
                }
            }
            for($ii=0;$ii<count($organize_list);$ii++){
                if($organize_list[$ii]['Hierarchy'] >= $i){
                    if($starth < $ii && $ii< $endh && $organize_list[$ii]['Hierarchy'] != $i){
                        if($dc != null && count($dc)>0){
                            foreach($dc as $c){
                                if($c[0]<$ii && $ii<$c[1]){
                                    $organize_list[$ii]['fontstyle'] .= $style[3];
                                }else{
                                    $organize_list[$ii]['fontstyle'] .= $style[1];
                                }
                            }
                        }else{
                            $organize_list[$ii]['fontstyle'] .= $style[1];
                        }
                        
                    }elseif($organize_list[$ii]['Hierarchy'] == $i){
                        if($ii == $endh){
                            $organize_list[$ii]['fontstyle'] .= $style[2];
                        }else{
                            $flag = true;
                            for($iii=$ii+1;$iii<count($organize_list);$iii++){
                                if($organize_list[$ii]['master'] == $organize_list[$iii]['master']){
                                    $flag = false;
                                }
                            }
                            if($flag){
                                $organize_list[$ii]['fontstyle'] .= $style[2];
                            }else{
                                $organize_list[$ii]['fontstyle'] .= $style[0];
                            }
                        }
                    }else{
                        $organize_list[$ii]['fontstyle'] .= $style[3]; 
                    }
                }
                
            }
            
        }
        $this->assign("organize",$organize_list);
        return $this->fetch();
    }

    public function add($og_id = 0){
        if($this->request->isPost()){
            $data = $this->request->post();
            if($data['og_name'] !=null && $data['master'] != null && $data['og_name'] != '' && $data['master'] != ''){
                if(OrganizeModel::where('og_id',$data['master'])->count()){
                    $flag = OrganizeModel::insert(['og_name'=>$data['og_name'],'master'=>$data['master']]);
                    if($flag){
                        return $this->success(lang('Update success!'), url('organize/index'));
                    }else{
                        return $this->error(lang('Update failed!'));
                    }
                }else{
                    return $this->error(lang('父级ID不存在!'));
                }
            }else{
                return $this->error(lang('Update failed!'));
            }
        }else{
            $organize = ['master' => $og_id];
            $this->assign('organize',$organize);
            return $this->fetch('edit');
        }
        
        
    }
    public function edit($og_id = 0){
        if($this->request->isPost()){
            $data = $this->request->post();
            if($og_id > 0 && $data['og_name'] !=null && $data['master'] != null && $data['og_name'] != '' && $data['master'] != ''){
                if(OrganizeModel::where('og_id',$og_id)->count() == 1){
                    $organize['master'] = $data['master'];
                    $organize['og_name'] = $data['og_name'];
                    $organize['og_id'] = $og_id;
                    if(OrganizeModel::where('og_id',$organize['master'])->count() == 1){
                        if(OrganizeModel::where('og_id',$og_id)->update($organize)){
                            $this->success(lang('Update success!'), url('organize/index'));
                        }else{
                            $this->error(lang('Update failed!'));
                        }
                    }else{
                        $this->error(lang('父级ID不存在!'));
                    }
                    
                }else{
                    $this->error(lang('Update failed!'));
                }
            }else{
                $this->error(lang('Update failed!'));
            }
        }else{
            if($og_id != 0){
                $organize = OrganizeModel::where('og_id',$og_id)->select();
                if(count($organize)>0){
                    $organize = $organize[0];
                    $this->assign('organize',$organize);
                    $this->assign('og_id',$og_id);
                    return $this->fetch();
                }else{
                    return $this->error('父组织不存在!');
                }
            }else{
                return $this->error('父组织不存在!');
            }
        }
    }
    public function delete($og_id){
        $flag = OrganizeModel::where('master',$og_id)->count();
        if($flag){
            return $this->error("禁止删除含有子组织的组织");
        }else{
            $flagg = OrganizeModel::where('og_id',$og_id)->delete();
            if($flagg){
                $this->success(lang('Delete success!'), url('organize/index'));
            }else{
                $this->error(lang('Delete failed!'));
            }

        }
    }
    public function search(){
        if($this->request->isPost()){
            $data = $this->request->post();
            if($data['key'] != null && $data['key'] != ''){
                $a = OrganizeModel::where('og_id',$data['key'])->select();
                $b = OrganizeModel::where('og_name','like','%'.$data['key'].'%')->select();
                if(count($a)>0 || count($b)>0){
                    $organize_list = array();
                    if(count($a)>0){
                        foreach($a as $i){
                            $i['fontstyle'] = '-->';
                            $organize_list[] = $i;
                        }
                    }
                    if(count($b)>0){
                        foreach($b as $i){
                            $i['fontstyle'] = '-->';
                            $organize_list[] = $i;
                        }
                    }
                    $this->assign('organize',$organize_list);
                    return $this->fetch('index');
                }else{
                    return $this->redirect(url('organize/index'));
                }
            }else{
                return $this->redirect(url('organize/index'));
            }
        }else{
            return $this->redirect(url('organize/index'));
        }
    }
}